/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stddef.h>
#include "drivers/MbedCRC_V2.h"

namespace mbed {

//SingletonPtr<PlatformMutex> mbed_crc_mutex;

/* Default values for different types of polynomials
*/
template<>
MbedCRC_V2<POLY_32BIT_ANSI, 32>::MbedCRC_V2():
    _initial_value(~(0x0)), _final_xor(~(0x0)), _reflect_data(true), _reflect_remainder(true),
    _crc_table((uint32_t *)Table_CRC_32bit_ANSI)
{
    mbed_crc_ctor();
}

template<>
MbedCRC_V2<POLY_32BIT_REV_ANSI, 32>::MbedCRC_V2():
    _initial_value(~(0x0)), _final_xor(~(0x0)), _reflect_data(false), _reflect_remainder(false),
    _crc_table((uint32_t *)Table_CRC_32bit_Rev_ANSI)
{
    mbed_crc_ctor();
}

template<>
MbedCRC_V2<POLY_16BIT_IBM, 16>::MbedCRC_V2():
    _initial_value(0), _final_xor(0), _reflect_data(true), _reflect_remainder(true),
    _crc_table((uint32_t *)Table_CRC_16bit_IBM)
{
    mbed_crc_ctor();
}

template<>
MbedCRC_V2<POLY_16BIT_CCITT, 16>::MbedCRC_V2():
    _initial_value(~(0x0)), _final_xor(0), _reflect_data(false), _reflect_remainder(false),
    _crc_table((uint32_t *)Table_CRC_16bit_CCITT)
{
    mbed_crc_ctor();
}

template<>
MbedCRC_V2<POLY_7BIT_SD, 7>::MbedCRC_V2():
    _initial_value(0), _final_xor(0), _reflect_data(false), _reflect_remainder(false),
    _crc_table((uint32_t *)Table_CRC_7Bit_SD)
{
    mbed_crc_ctor();
}

template<>
MbedCRC_V2<POLY_8BIT_CCITT, 8>::MbedCRC_V2():
    _initial_value(0), _final_xor(0), _reflect_data(false), _reflect_remainder(false),
    _crc_table((uint32_t *)Table_CRC_8bit_CCITT)
{
    mbed_crc_ctor();
}

extern const uint32_t Table_CRC_32bit_Rev_ANSI[MBED_OPTIMIZED_CRC_TABLE_SIZE] = {
    0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};

extern const uint16_t crc16talbeabs[MBED_OPTIMIZED_CRC_TABLE_SIZE] = {
    0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
    0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
};

} // namespace mbed
